package com.ota.client.tools;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Hash {
    MessageDigest digest;
    String alg;

    public static Hash SHA256=createHash("SHA-256");
    public static Hash SHA512=createHash("SHA-512");
    public static Hash createHash(String alg){
        return new Hash(alg);
    }
	public  Hash(String alg){
		try {
            this.alg = alg;
			digest = MessageDigest.getInstance(alg); 
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
	}

	public void reset() {
		digest.reset();
	}


	public void write(byte[] bytes) {
		digest.update(bytes);
	}


	public byte[] sum(byte[] bytes) {
		if(bytes!=null){
			digest.update(bytes);
		}
		return digest.digest();
	}

	public int size() {
		return 64;
	}

	public Hash getHash() {
		// TODO Auto-generated method stub
		return createHash(alg);
	}

	public int blockSize() {
		// TODO Auto-generated method stub
		return 128;
	}

	public void write(byte[] bytes, int off, int len) {
		// TODO Auto-generated method stub
		digest.update(bytes, off, len);
		
	}
	

	public void write(byte b) {
		digest.update(b);
	}

}
